package pl.rgolcz.antnet.testcases;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import pl.rgolcz.antnet.AntNet;
import pl.rgolcz.antnet.DijkstraShortestPath;
import pl.rgolcz.antnet.GraphReader;
import pl.rgolcz.antnet.IWeightedEdge;
import pl.rgolcz.antnet.Main;
import edu.uci.ics.jung.graph.Graph;

public class SolutionVsIter
{
    public static void main(String[] s) throws IOException
    {

        /* Get input params */

        // graph source [file|random]
        // root vertex
        // dest vertex
        // visualization
        int root = 5;
        int dest = 21;
        /*
         * int root = 0;
         * int dest = 6;
         */

        System.out.println("Root vertex: " + root);
        System.out.println("Dest vertex: " + dest);

        Graph<Integer, IWeightedEdge> graph = null;

        /* Load the graph definition from file */
        try
        {
            graph = getGraph();
        }
        catch (IOException e)
        {
            System.out.println("Failed to load the graph");
            e.printStackTrace();
            System.exit(1);
        }

        /* Run Dijkstra algorithm */
        System.out.println("\n-- Running Dijkstra algorithm --");
        DijkstraShortestPath<Integer> dijkstra = new DijkstraShortestPath<>(graph, root);
        dijkstra.run();
        List<Integer> dijkstraShortestPath = dijkstra.getShortestPath(dest);
        System.out.println("Dijkstra shortest path: " + dijkstraShortestPath);
        System.out.println("Dijkstra shortest path length: " + dijkstra.getShortestPathLength(dest));

        /* Run ant algorithm */
        List<AntNet> results = new ArrayList<>(30);
        for(int i = 0; i < 30; i++)
        {
            System.out.println("\n-- Running Ant algorithm --");
            AntNet antnet = new AntNet(graph, root, dest);
            antnet.run();
            results.add(antnet);
        }
        
        /* Write results */
        File out = new File("SolutionVsIter.txt");
        FileWriter writer = new FileWriter(out);
        for(int i = 0; i < results.get(0).getSolutionLengthList().size(); i++)
        {
            writer.write(i + " ");
            for(AntNet net : results)
            {
                if(net.getSolutionLengthList().get(i) != null)
                {
                    writer.write(String.format("%5.2f ", net.getSolutionLengthList().get(i)).replace(',', '.'));
                }
                else
                {
                    writer.write("  -   ");
                }
            }
            writer.write("\n");
        }
        writer.close();
    }
    
    /**
     * @return the graph for this demo
     * @throws IOException 
     */
    static Graph<Integer, IWeightedEdge> getGraph() throws IOException {

        //BufferedReader reader = new BufferedReader(new FileReader(new File("graph.net")));
        BufferedReader reader = new BufferedReader(new FileReader(new File("random.net")));
        
        GraphReader graphReader = new GraphReader(reader);
        Graph<Integer, IWeightedEdge> graph = graphReader.read();
        return graph;
    }
}
